package cn.kp.learning.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.kp.learning.model.Organization;
import cn.kp.learning.model.OrganizationExample;
import cn.kp.learning.service.OrganizationService;
import cn.kp.learning.util.ResponseUtil;

@Controller
@RequestMapping("/organization")
public class OrganizationController {


	@Resource
	private OrganizationService organizationService;
	
	@RequestMapping("/orgAdd")
	public void orgAdd(HttpServletResponse response,HttpServletRequest request, Organization org ){
		JSONObject json=new JSONObject();
		System.out.println("org add :" + org.getOrgCompany());
		int orgaddresult = organizationService.addOrganization(org);
		System.out.println("orgadd Action : "+orgaddresult);
		if(orgaddresult>0){
			json.put("success", true);
		}else{
			json.put("success", false);
		}
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/organizationList")
	public void organizationList(HttpServletResponse response,HttpServletRequest request, Organization org ){
		JSONObject json=new JSONObject();
		System.out.println("get people list ... ");
		System.out.println("get para : " + request.getParameter("opportunityNO"));
		System.out.println("get para : " + request.getParameter("keyword"));
		System.out.println("get para : " + request.getParameter("search0"));
		System.out.println("get para : " + request.getParameter("search1"));
		System.out.println("get para : " + request.getParameter("search2"));
		System.out.println("get para : " + request.getParameter("search3"));
		System.out.println("get para : " + request.getParameter("search4"));
		System.out.println("get para : " + request.getParameter("search5"));
		System.out.println("get para : " + request.getParameter("limit"));
		System.out.println("get para : " + request.getParameter("start"));
		System.out.println("get para : " + request.getParameter("page"));
		System.out.println("get para : " + request.getParameter("pages"));

		List<Organization> lt = new ArrayList<Organization>();
		OrganizationExample orgExm = new OrganizationExample();
		
		String startRows = request.getParameter("start");
		String pagesLimit = request.getParameter("limit");
		if(null!=startRows && !startRows.equals("")){
			orgExm.setStartRow(Integer.parseInt(startRows));
		}
		if(null!=pagesLimit && !pagesLimit.equals("")){
			orgExm.setPageSize(Integer.parseInt(startRows) + Integer.parseInt(pagesLimit));
		}
		
		System.out.println("org add :" + org.getOrgCompany());
		orgExm.setDistinct(true);
		orgExm.setOrderByClause("org_company_id");
		lt = organizationService.selectByExample(orgExm);
		int total = organizationService.countByExample(orgExm);
		System.out.println("orgadd Action : "+lt.size());
		try {
			JSONArray jsonArray = JSONArray.fromObject(lt);
			json.put("total", total);
			json.put("pages", total);
			json.put("rows", jsonArray);
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/getOrganization")
	public void getOrganization(HttpServletResponse response,HttpServletRequest request, Organization org ){
		JSONObject json=new JSONObject();
		System.out.println("get people list ... ");
		System.out.println("get para : " + request.getParameter("opportunityNO"));
		System.out.println("get para : " + request.getParameter("keyword"));
		System.out.println("get para : " + request.getParameter("search0"));
		System.out.println("get para : " + request.getParameter("search1"));
		System.out.println("get para : " + request.getParameter("search2"));
		System.out.println("get para : " + request.getParameter("search3"));
		System.out.println("get para : " + request.getParameter("search4"));
		System.out.println("get para : " + request.getParameter("search5"));
		System.out.println("get para : " + request.getParameter("limit"));
		System.out.println("get para : " + request.getParameter("start"));
		System.out.println("get para : " + request.getParameter("page"));
		System.out.println("get para : " + request.getParameter("pages"));

		List<Organization> lt = new ArrayList<Organization>();
		OrganizationExample orgExm = new OrganizationExample();
		orgExm.createCriteria().andOrgIdEqualTo(org.getOrgId());
		String startRows = request.getParameter("start");
		String pagesLimit = request.getParameter("limit");
		if(null!=startRows && !startRows.equals("")){
			orgExm.setStartRow(Integer.parseInt(startRows));
		}
		if(null!=pagesLimit && !pagesLimit.equals("")){
			orgExm.setPageSize(Integer.parseInt(startRows) + Integer.parseInt(pagesLimit));
		}
		
		System.out.println("org add :" + org.getOrgCompany());
		orgExm.setDistinct(true);
		orgExm.setOrderByClause("org_company_id");
		int total = organizationService.countByExample(orgExm);
		//if records > 1
		if(total>1){
			
		}else{
			lt = organizationService.selectByExample(orgExm);
			json = JSONObject.fromObject(lt.get(0));
		}
		System.out.println("orgadd Action : " );
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/getOrgCompany")
	public void getOrgCompany(HttpServletResponse response,HttpServletRequest request){
		List<Organization> lt = organizationService.selectCompany(null);
		//
		JSONObject json=new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(lt);
		json.put("total", lt.size());
		json.put("pages", lt.size()/10+1);
		json.put("rows", jsonArray);
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/getOrgDepartment")
	public void getOrgDepartment(HttpServletResponse response,HttpServletRequest request,String companyName,String companyId){
		HashMap<String,Object> hm = new HashMap<String, Object>();
		hm.put("orgCompany", companyName);
		hm.put("orgCompanyId", companyId);
		List<Organization> lt = organizationService.selectDepartment(hm);
		/*List<Organization> lt = new ArrayList<Organization>();
		if(null==companyId ||companyId.equals("")){
			
		}else{
			lt = organizationService.selectDepartment(hm);
		}*/
		//
		JSONObject json=new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(lt);
		json.put("total", lt.size());
		json.put("pages", lt.size()/10+1);
		json.put("rows", jsonArray);
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/getOrgOffice")
	public void getOrgOffice(HttpServletResponse response,HttpServletRequest request,String companyName,String companyId,String departmentName,String departmentId){
		HashMap<String,Object> hm = new HashMap<String, Object>();
		hm.put("orgCompany", companyName);
		hm.put("orgCompanyId", companyId);
		hm.put("orgDepartment", departmentName);
		hm.put("orgDepartmentId", departmentId);
		List<Organization> lt = organizationService.selectOffice(hm);
		
		//
		JSONObject json=new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(lt);
		json.put("total", lt.size());
		json.put("pages", lt.size()/10+1);
		json.put("rows", jsonArray);
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/getOrgGroup")
	public void getOrgGroup(HttpServletResponse response,HttpServletRequest request,String companyName,String companyId,String departmentName,String departmentId,String officeName,String officeId){
		HashMap<String,Object> hm = new HashMap<String, Object>();
		hm.put("orgCompany", companyName);
		hm.put("orgCompanyId", companyId);
		hm.put("orgDepartment", departmentName);
		hm.put("orgDepartmentId", departmentId);
		hm.put("orgOffice", officeName);
		hm.put("orgOfficeId", officeId);
		List<Organization> lt = organizationService.selectGroup(hm);
		//
		JSONObject json=new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(lt);
		json.put("total", lt.size());
		json.put("pages", lt.size()/10+1);
		json.put("rows", jsonArray);
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
